import { addSave } from '@/api/code'
import { ref } from 'vue'

/**
 * 创建条码
 */
export const useCreateCode = () => {
  const createLoading = ref(false)
  const createForm = ref<CodeAPIRequest.Create>({
    saveVoList: [],
  })

  const resetForm = () => {
    createForm.value.saveVoList.length = 0
  }

  const formSave = async () => {
    try {
      createLoading.value = true
      const res = await addSave(createForm.value)
      createLoading.value = false
      return res.data
    } catch (error) {
      createLoading.value = false
      return {
        code: 10000,
        message: '创建条码失败，请稍后重试',
        data: {},
        success: false,
      }
    }
  }

  return {
    createForm,
    createLoading,
    formSave,
    resetForm,
  }
}
